package com.yugao.fintech.antelope.shop.model.entity;

import com.alibaba.fastjson2.JSON;
import com.baomidou.mybatisplus.annotation.TableField;
import com.baomidou.mybatisplus.annotation.TableName;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.baomidou.mybatisplus.extension.handlers.AbstractJsonTypeHandler;
import com.fasterxml.jackson.annotation.JsonFormat;
import com.yugao.fintech.antelope.base.model.BasePlusPO;
import com.yugao.fintech.antelope.base.model.constants.CommonCons;
import com.yugao.fintech.antelope.base.model.constants.ValidationCons;
import com.yugao.fintech.antelope.base.tenant.core.DataIsolation;
import lombok.Data;
import lombok.Getter;
import lombok.Setter;
import org.springframework.format.annotation.DateTimeFormat;

import javax.validation.constraints.NotNull;
import java.math.BigDecimal;
import java.time.LocalTime;
import java.util.List;

/**
 * 门店信息
 *

 */
@Getter
@Setter
@TableName(value = "shop_store", autoResultMap = true)
@DataIsolation(filterMainTenant = true, tableName = "shop_store")
public class ShopStore extends BasePlusPO {
	private Long id;
	
	/**
	 * 名称
	 */
	private String name;
	
	/**
	 * logo
	 */
	private String logo;
	
	/**
	 * 地址
	 */
	private String address;

	/**
	 * 地址详情
	 */
	@TableField(typeHandler = AddressDetailHandler.class)
	private AddressDetail addressDetail;
	
	/**
	 * 状态, 禁用/启用
	 * @see CommonCons.UseStats
	 */
	private String status;
	
	/**
	 * 省
	 */
	private Long provinceId;
	
	/**
	 * 市
	 */
	private Long cityId;
	
	/**
	 * 区
	 */
	private Long regionId;
	
	/**
	 * 备注
	 */
	private String remark;
	
	/**
	 * 营业时间, json格式
	 */
	@TableField(typeHandler = OpenTimeTypeHandler.class)
	private List<OpenTime> openTime;
	
	/**
	 * 营业状态, 1： 营业中, 0: 停止营业
	 */
	private String openStatus;

	/**
	 * 员工数量
	 */
	private Integer employeeNumber;

	/**
	 * 门店标签, 多个之间用逗号隔开
	 */
	private String tags;

	/**
	 * 门店电话
	 */
	private String phone;

	/**
	 * 主租户id
	 */
	private Long mainTenantId;

	/**
	 * 租户id, 一个门店对应一个租户
	 */
	private Long tenantId;

    public static LambdaQueryWrapper<ShopStore> lqw() {
		return Wrappers.lambdaQuery();
	}

	@Data
	public static class AddressLocation {
		private BigDecimal lat;
		private BigDecimal lng;
	}
	/**
	 * 地址详情
	 */
	@Data
	public static class AddressDetail {
		private AddressLocation location;
	}

	/**
	 * 店铺开放时间

	 */
	@Data
	public static class OpenTime {
		@NotNull(message = ValidationCons.NOT_EMPTY)
		private List<String> openWeek;

		@NotNull(message = ValidationCons.NOT_EMPTY)
		@DateTimeFormat(pattern = "HH:mm")
		@JsonFormat(timezone = "GMT+8", pattern = "HH:mm")
		private LocalTime startTime;

		@NotNull(message = ValidationCons.NOT_EMPTY)
		@DateTimeFormat(pattern = "HH:mm")
		@JsonFormat(timezone = "GMT+8", pattern = "HH:mm")
		private LocalTime endTime;
	}

	public static class OpenTimeTypeHandler extends AbstractJsonTypeHandler<List<OpenTime>> {

		@Override
		protected List<OpenTime> parse(String json) {
			return JSON.parseArray(json, OpenTime.class);
		}

		@Override
		protected String toJson(List<OpenTime> obj) {
			return JSON.toJSONString(obj).trim();
		}
	}

	public static class AddressDetailHandler extends AbstractJsonTypeHandler<AddressDetail> {

		@Override
		protected AddressDetail parse(String json) {
			return JSON.parseObject(json, AddressDetail.class);
		}

		@Override
		protected String toJson(AddressDetail obj) {
			return JSON.toJSONString(obj).trim();
		}
	}

}
